package com.example.demo6.ui.home;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.app.AlertDialog;
import android.content.DialogInterface;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.example.demo6.Account;
import com.example.demo6.R;

public class HomeFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        TextView textViewPickup = view.findViewById(R.id.topickupdetail);
        textViewPickup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击"+拾物"后的处理逻辑
//                NavController navController = Navigation.findNavController(v);
//                Bundle bundle=new Bundle();
//                bundle.putString("username", Account.getInstance().getCurrentUsername());
//                navController.navigate(R.id.edit_pickup_informationFragment); // 假设pickupFragment为目标fragment的id
                String username = Account.getInstance().getCurrentUsername();
                if (TextUtils.isEmpty(username)) {
                    showLoginPrompt(v); // 如果没有获取到用户名，显示登录提示对话框
                } else {
                    NavController navController = Navigation.findNavController(v);
                    Bundle bundle = new Bundle();
                    bundle.putString("username", username);
                    navController.navigate(R.id.edit_pickup_informationFragment, bundle); // 假设edit_pickup_informationFragment为目标fragment的id
                }
            }
        });

        TextView textViewLost = view.findViewById(R.id.tolostdetail);
        textViewLost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击"+失物"后的处理逻辑
                String username = Account.getInstance().getCurrentUsername();
                if (TextUtils.isEmpty(username)) {
                    showLoginPrompt(v); // 如果没有获取到用户名，显示登录提示对话框
                } else {
                    NavController navController = Navigation.findNavController(v);
                    Bundle bundle = new Bundle();
                    bundle.putString("username", Account.getInstance().getCurrentUsername());
                    navController.navigate(R.id.blankFragment); // 假设lostFragment为目标fragment的id
                }
            }
        });

    }
    // 新增一个方法用于导航到登录界面
    private void showLoginPrompt(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
        builder.setTitle("提示")
                .setMessage("您当前未登录，请先登录。")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        NavController navController = Navigation.findNavController(v);
                        navController.navigate(R.id.loginFragment); // 导航到登录界面
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        NavController navController = Navigation.findNavController(v);
                        navController.navigate(R.id.navigation_home); // 导航回到HomeFragment
                    }
                })
                .show();
    }
}